import React, { Component } from 'react';
import { connect } from 'dva';
import { message, Modal, Button } from 'antd';
import styles from './../Users.css';
import moment from 'moment';
import Factory from '../../factory';
import * as datetime from '../../../utils/datetime'
import dataEditCreate from '../../addons/DataEdit'
import dataTableCreate from '../../addons/DataTable'
import dataImportCreate from '../../addons/DataImport'
import * as menuService from '../../../services/menus';
import numeral from 'numeral';

const daoName = "COMP_JC_DR";
const ButtonGroup = Button.Group;
const columns = [
  { title: '序号', width: 100, sorter: true, dataIndex: 'ID', key: 'ID', dispaly: true, filter: true, export: false },
  { title: '任务序号', width: 120, sorter: true, dataIndex: 'RWXH', key: 'RWXH', dispaly: true, filter: true, export: false },
  { title: '监测任务名称', width: 250, sorter: true, dataIndex: "JCRWMC", key: 'JCRWMC', dispaly: true, filter: true, export: true },
  { title: '所属条线', width: 150, sorter: true, dataIndex: "SSTX", key: 'SSTX', dispaly: true, filter: true, export: true },
  { title: '收益承诺日期', width: 180, sorter: true, dataIndex: "SYCRQ", key: 'SYCRQ', dispaly: true, filter: true, export: true },
  { title: '经营单位', width: 150, sorter: true, dataIndex: "JYDW", key: 'JYDW', dispaly: true, filter: true, export: true },
  { title: '负责人', width: 120, sorter: true, dataIndex: "FZR", key: 'FZR', dispaly: true, filter: true, export: true },
  { title: '客户经理1', width: 120, sorter: true, dataIndex: "KHJL1", key: 'KHJL1', dispaly: true, filter: true, export: true },
  { title: '客户经理2', width: 120, sorter: true, dataIndex: "KHJL2", key: 'KHJL2', dispaly: true, filter: true, export: true },
  { title: '客户经理3', width: 120, sorter: true, dataIndex: "KHJL3", key: 'KHJL3', dispaly: true, filter: true, export: true },
  { title: '客户经理4', width: 120, sorter: true, dataIndex: "KHJL4", key: 'KHJL4', dispaly: true, filter: true, export: true },
  { title: '客户经理5', width: 120, sorter: true, dataIndex: "KHJL5", key: 'KHJL5', dispaly: true, filter: true, export: true },
  { title: '客户名称', width: 250, sorter: true, dataIndex: "KHMC", key: 'KHMC', dispaly: true, filter: true, export: true },
  { title: '关联客户名称', width: 300, sorter: true, dataIndex: "GLKHMC", key: 'GLKHMC', dispaly: true, filter: true, export: true },
  { title: '账号', width: 200, sorter: true, dataIndex: "ZH", key: 'ZH', dispaly: true, filter: true, export: true },
  { title: '监测类型', width: 180, sorter: true, dataIndex: "JCLX", key: 'JCLX', dispaly: true, filter: true, export: true },
  { title: '监测起始日', width: 150, sorter: true, key: 'JCQSR', caption: "JCQSR", render: data => datetime.format(data.JCQSR, "yyyy-MM-dd"), export: true, dispaly: true, filter: true },
  { title: '监测截止日', width: 150, sorter: true, key: 'JCJZR', caption: "JCJZR", render: data => datetime.format(data.JCJZR, "yyyy-MM-dd"), export: true, dispaly: true, filter: true },
  { title: '承诺金额', width: 180, sorter: true, dataIndex: "CRJE", key: 'CRJE', dispaly: true, filter: true, export: true },
  { title: '承诺税前利率', width: 180, sorter: true, dataIndex: "CRSQLL", key: 'CRSQLL', dispaly: true, filter: true, export: true },
  { title: '备注', width: 180, sorter: true, dataIndex: "BZ", key: 'BZ', dispaly: true, filter: true, export: true },
];
const controls = [
  { name: "ID", label: "序号", type: "text", grid: 12 / 24 },
  { name: "RWXH", label: "任务序号", type: "text", grid: 12 / 24 },
  { name: "JCRWMC", label: "监测任务名称", type: "text", grid: 12 / 24 },
  { name: "SSTX", label: "所属条线", type: "text", grid: 12 / 24 },
  { name: "SYCRQ", label: "收益承诺日期", type: "text", grid: 12 / 24 },
  { name: "JYDW", label: "经营单位", type: "text", grid: 12 / 24 },
  { name: "FZR", label: "负责人", type: "text", grid: 12 / 24 },
  { name: "KHJL1", label: "客户经理1", type: "text", grid: 12 / 24 },
  { name: "KHJL2", label: "客户经理2", type: "text", grid: 12 / 24 },
  { name: "KHJL3", label: "客户经理3", type: "text", grid: 12 / 24 },
  { name: "KHJL4", label: "客户经理4", type: "text", grid: 12 / 24 },
  { name: "KHJL5", label: "客户经理5", type: "text", grid: 12 / 24 },
  { name: "KHMC", label: "客户名称", type: "text", grid: 12 / 24 },
  { name: "GLKHMC", label: "关联客户名称", type: "text", grid: 12 / 24 },
  { name: "ZH", label: "账号", type: "text", grid: 12 / 24 },
  { name: "JCLX", label: "监测类型", type: "text", grid: 12 / 24 },
  { name: "JCQSR", label: "监测起始日", type: "date", dataType: "date", grid: 12 / 24 },
  { name: "JCJZR", label: "监测截止日", type: "date", dataType: "date", grid: 12 / 24 },
  { name: "CRJE", label: "承诺金额", type: "text", grid: 12 / 24 },
  { name: "CRSQLL", label: "承诺税前利率", type: "text", grid: 12 / 24 },
  { name: "BZ", label: "备注", type: "text", grid: 12 / 24 },
  { name: 'DRJG', required: false, ignore: true, type: 'text', label: '导入结果', grid: 12 / 24 },
];

const controlsDtip = [
  { name: "JCRWMC", label: "监测任务名称", type: "text", grid: 12 / 24 },
  { name: "SSTX", label: "所属条线", type: "text", grid: 12 / 24 },
  { name: "SYCRQ", label: "收益承诺日期", type: "text", grid: 12 / 24 },
  { name: "JYDW", label: "经营单位", type: "text", grid: 12 / 24 },
  { name: "FZR", label: "负责人", type: "text", grid: 12 / 24 },
  { name: "KHJL1", label: "客户经理1", type: "text", grid: 12 / 24 },
  { name: "KHJL2", label: "客户经理2", type: "text", grid: 12 / 24 },
  { name: "KHJL3", label: "客户经理3", type: "text", grid: 12 / 24 },
  { name: "KHJL4", label: "客户经理4", type: "text", grid: 12 / 24 },
  { name: "KHJL5", label: "客户经理5", type: "text", grid: 12 / 24 },
  { name: "KHMC", label: "客户名称", type: "text", grid: 12 / 24 },
  { name: "GLKHMC", label: "关联客户名称", type: "text", grid: 12 / 24 },
  { name: "ZH", label: "账号", type: "text", grid: 12 / 24 },
  { name: "JCLX", label: "监测类型", type: "text", grid: 12 / 24 },
  { name: "JCQSR", label: "监测起始日", type: "date", dataType: "date", grid: 12 / 24 },
  { name: "JCJZR", label: "监测截止日", type: "date", dataType: "date", grid: 12 / 24 },
  { name: "CRJE", label: "承诺金额", type: "text", grid: 12 / 24 },
  { name: "CRSQLL", label: "承诺税前利率", type: "text", grid: 12 / 24 },
  { name: "BZ", label: "备注", type: "text", grid: 12 / 24 },
];
const DataTable = dataTableCreate(daoName);
const DataImport = dataImportCreate(daoName);
class COMP_JC_DRForm extends Component {
  constructor(props) {
    super(props);
    this.state = {
      dataChange: false,
      visible: false
    };
  }
  componentDidMount() {
    this.props.dispatch({
      type: `${daoName}/setKeyName`,
      payload: { keyName: "ID", permID: this.props.permID }
    });
    let sort = {
      field: 'ID',
      order: 'asc'
    }
    this.props.dispatch({
      type: `${daoName}/fetch`,
      payload: { daoName, page: 1, pageSize: 20, sorter: sort }
    });
    this.loadOptionData();
  }
  componentWillReceiveProps(nextProps) {
    const { showImport } = this.props
    if (!nextProps.showImport && showImport) {
      this.props.dispatch({
        type: `${daoName}/fetch`,
        payload: { daoName, page: 1, pageSize: 20 }
      });
    }
  }
  loadOptionData() {
    let options = []
    controls.map(c => {
      if (c.fetch) {
        let f = c.fetch;
        if (c.type === 'cascader-select') {
          f.isCascader = true;
        }
        if (c.type !== 'search') {
          options.push(f);
        }
      }
    })
    if (daoName && options.length > 0) {
      this.props.dispatch({ type: `${daoName}/fetchOptions`, payload: { options } });
    }
  }
  showImportHandler() {
    this.props.dispatch({
      type: `${daoName}_DataImport/setDisplay`,
      payload: { display: true },
    });
  };
  DownTmportHandler() {
    let columnHeaders = []
    columns.map((c, j) => {
      if (!c.export) {
        return
      }
      let header = {
        Name: c.key,
        Caption: c.title,
        Width: c.width
      }
      columnHeaders.push(header)
    })
    let form = document.createElement('form');//定义一个form表单
    let whereItems = [];
    whereItems.push({
      FieldName: "1",
      Operator: "<>",
      FieldValues: "1"
    })
    const whereCauses = []
    whereCauses.push({
      WhereItems: whereItems,
      RelationSign: "AND"
    })
    const orderItems = []
    form.style = "display:none";
    form.target = "_blank";
    form.method = "post";
    form.action = "/api/ExportXlsx/" + daoName;
    let input1 = document.createElement("input");
    input1.type = "hidden";
    input1.name = "WhereCauses";
    input1.value = JSON.stringify(whereCauses);
    let input2 = document.createElement("input");
    input2.type = "hidden";
    input2.name = "OrderItems";
    input2.value = JSON.stringify(orderItems)
    let input3 = document.createElement("input");
    input3.type = "hidden";
    input3.name = "ColumnHeaders";
    input3.value = JSON.stringify(columnHeaders);
    let input4 = document.createElement("input");
    input4.type = "hidden";
    input4.name = "PermID";
    input4.value = "" + this.props.permID;
    let input5 = document.createElement("input");
    input5.type = "hidden";
    input5.name = "UserID";
    input5.value = "" + this.props.userID;
    document.body.appendChild(form);//将表单放置在web中
    form.appendChild(input1);
    form.appendChild(input2);
    form.appendChild(input3);
    form.appendChild(input4);
    form.appendChild(input5);
    form.submit();
    document.body.removeChild(form)
  }


  render() {
    let { showEdit, record, showImport, permID, popedomTypes } = this.props
    const pTypes = popedomTypes[permID]
    let data = <ButtonGroup style={{ zIndex: 0 }}>
      <Button onClick={this.showImportHandler.bind(this)}>导入</Button>
      <Button onClick={this.DownTmportHandler.bind(this)}>下载导入模板</Button>
    </ButtonGroup>
    return (
      <div className={styles.normal} style={{ background: '#fff', marginTop: -16, padding: '5px 5px' }}>
        <div>
          <div className={styles.create}>
            {data}
          </div>
          <DataTable ref="WBTable" daoName={daoName} columns={columns} controls={controls} permID={permID} />
          <DataImport title="重点客户导入" importName="COMP_JC_DR" daoName="COMP_JC_DR" importMode="TruncteAfterInsert" controls={controlsDtip} forbidConvert={true} saveByDao={true} />
        </div>
      </div>
    )
  }
}
function mapStateToProps(state) {
  const { showEdit, record, selectedRowKeys, keyName } = state[daoName];
  const { userID, orgID, pOrgID, popedomTypes } = state.menus;
  const { display } = state[`${daoName}_DataImport`];
  return {
    userID,
    orgID,
    pOrgID,
    popedomTypes,
    showEdit,
    selectedRowKeys,
    keyName,
    record,
    showImport: display
  }
}
COMP_JC_DRForm = connect(mapStateToProps)(COMP_JC_DRForm)
export default COMP_JC_DRForm;

let render = (props) => React.createElement(COMP_JC_DRForm, props);
let f = new Factory()
f.set("COMP_JC_DRForm", render)
